package pt.ua.helpdesk.client;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class UserRemove extends Composite {
	private Integer IDUser ;
	
	public UserRemove(Integer idUser, String uname) {

		this.IDUser = idUser ;
		
		final VerticalPanel verticalPanel = new VerticalPanel();
		initWidget(verticalPanel);
		verticalPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		verticalPanel.setSpacing(5);

		final Label lblPerg = new Label("");
		verticalPanel.add(lblPerg);
		lblPerg.setStyleName("label");
		lblPerg.setText("Tem a certeza que pretende desactivar o utilizador " + uname +  "?");

		final HorizontalPanel horizontalPanel = new HorizontalPanel();
		verticalPanel.add(horizontalPanel);
		horizontalPanel.setSpacing(5);

		final Button btnSim = new Button();
		horizontalPanel.add(btnSim);
		btnSim.setStyleName("button");
		btnSim.setText("Sim");

		final Button btnNao = new Button();
		horizontalPanel.add(btnNao);
		btnNao.setStyleName("button");
		btnNao.setText("Não");
		btnNao.addClickListener(new ClickListener(){
			public void onClick(Widget sender) {
				Default.loadComposite(new UserSearch());
			}
		});

		final Label lblAlerta = new Label("");
		verticalPanel.add(lblAlerta);
		lblAlerta.setVisible(false);
		lblAlerta.setStyleName("label");

		final Button btnVoltar = new Button();
		verticalPanel.add(btnVoltar);
		btnVoltar.setVisible(false);
		btnVoltar.setStyleName("buttonVoltar");
		btnVoltar.setText("< Voltar");
		btnVoltar.addClickListener(new ClickListener(){
			public void onClick(Widget sender) {
				Default.loadComposite(new UserSearch());
			}
		});
		
		btnSim.addClickListener(new ClickListener(){
			public void onClick(Widget sender) {
				lblPerg.setVisible(false);
				horizontalPanel.setVisible(false);
				ServiceAdministration.Util.getInstance().removeUser(IDUser, new UserSearchAsyncCallback(lblAlerta));
				lblAlerta.setVisible(true);
				btnVoltar.setVisible(true);
			}
		});
	}
	
	static class UserSearchAsyncCallback implements AsyncCallback
	{
		Label lbl ;
		UserSearchAsyncCallback(Label lbl)
		{
			this.lbl = lbl ;
		}
		
		public void onFailure(Throwable caught) {
			System.out.println("UserRemove :: FALHOU");
		}

		public void onSuccess(Object result) {
			Boolean OK = (Boolean)result;
			String message;
	    	if(OK.booleanValue())
	    	{
	    		message = "Utilizador removido com sucesso";
	    		DOM.setInnerHTML(lbl.getElement(), message);
	    	}
	    	else
	    	{
	    		message = "Ocorreu um erro inesperado";
	    		DOM.setInnerHTML(lbl.getElement(), message);
	    	}
			
		}
	}
}
